﻿using Entity = Corporation.Project.Entities.Article;

namespace Corporation.Project.Events
{
    /// <summary>
    /// Demo 事件处理器
    /// </summary>
    public class ArticleEventHandler : EventHandlerBase<EntityEventData<Entity>>
    {
        //字段
        private readonly IServiceProvider _provider;

        /// <summary>
        /// 初始化一个Demo事件处理器（<see cref="ArticleEventHandler"/>）的新实例
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public ArticleEventHandler(IServiceProvider provider)
        {
            _provider = provider;
        }

        /// <summary>
        /// 获取 日志对象
        /// </summary>
        protected ILogger Logger => _provider.GetLogger(GetType());

        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        public override void Handle(EntityEventData<Entity> eventData, IServiceProvider provider)
        {
            switch (eventData.OperateType)
            {
                case OperateType.Insert:
                    Logger.LogDebug("创建了编号为 {id} 的 {name} 数据", eventData.Entities.Select(ul => ul.Id.ToString().ExpandAndToString()), nameof(Entity));
                    break;
                case OperateType.Update:
                    Logger.LogDebug("更新了编号为 {id} 的 {name} 数据", eventData.Entities.Select(ul => ul.Id.ToString().ExpandAndToString()), nameof(Entity));
                    break;
                case OperateType.Delete:
                    Logger.LogDebug("删除了编号为 {id} 的 {name} 数据", eventData.Entities.Select(ul => ul.Id.ToString().ExpandAndToString()), nameof(Entity));
                    break;
            }
        }
    }
}
